#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class ScoreInPratiseProvider : CY.GFive.Core.Providers.DALProvider.IScoreInPratiseProvider
	{
		#region Sql
		private static readonly string SqlSelectScoreInPratise = "SELECT [Id], [StudentCode],[YearNum],[Term],[ScoreNum],[ScoreCate] FROM [ScoreInPratise] WHERE  [Id]=@Id";

		private static readonly string SqlInsertScoreInPratise = "INSERT INTO [ScoreInPratise]([StudentCode],[YearNum],[Term],[ScoreNum],[ScoreCate]) values(@StudentCode,@YearNum,@Term,@ScoreNum,@ScoreCate);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateScoreInPratise = "UPDATE [ScoreInPratise] SET [StudentCode] = @StudentCode,[YearNum] = @YearNum,[Term] = @Term,[ScoreNum] = @ScoreNum,[ScoreCate] = @ScoreCate WHERE  [Id]=@Id";

		private static readonly string SqlDeleteScoreInPratise = "DELETE [ScoreInPratise] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllScoreInPratise = "SELECT [Id], [StudentCode],[YearNum],[Term],[ScoreNum],[ScoreCate] FROM [ScoreInPratise]";
		#endregion

		#region IScoreInPratiseProvider Members
		public Core.Business.ScoreInPratise Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectScoreInPratise);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.ScoreInPratise scoreInPratise = new Core.Business.ScoreInPratise();

				if (!reader.IsDBNull(0)) scoreInPratise.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) scoreInPratise.StudentCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) scoreInPratise.YearNum = reader.GetString(2);
				if (!reader.IsDBNull(3)) scoreInPratise.Term = reader.GetInt32(3);
				if (!reader.IsDBNull(4)) scoreInPratise.ScoreNum = reader.GetDecimal(4);
				if (!reader.IsDBNull(5)) scoreInPratise.ScoreCate = reader.GetString(5);

				return scoreInPratise;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.ScoreInPratise scoreInPratise)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StudentCode", SqlDbType.NVarChar, scoreInPratise.StudentCode);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, scoreInPratise.YearNum);
			sql.AddParameter("@Term", SqlDbType.Int, scoreInPratise.Term);
			sql.AddParameter("@ScoreNum", SqlDbType.Decimal, scoreInPratise.ScoreNum);
			sql.AddParameter("@ScoreCate", SqlDbType.NVarChar, scoreInPratise.ScoreCate);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertScoreInPratise);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) scoreInPratise.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.ScoreInPratise scoreInPratise)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StudentCode", SqlDbType.NVarChar, scoreInPratise.StudentCode);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, scoreInPratise.YearNum);
			sql.AddParameter("@Term", SqlDbType.Int, scoreInPratise.Term);
			sql.AddParameter("@ScoreNum", SqlDbType.Decimal, scoreInPratise.ScoreNum);
			sql.AddParameter("@ScoreCate", SqlDbType.NVarChar, scoreInPratise.ScoreCate);
			sql.AddParameter("@Id", SqlDbType.Int, scoreInPratise.Id);
			sql.ExecuteSql(SqlUpdateScoreInPratise);
		}

		public void Delete(Core.Business.ScoreInPratise scoreInPratise)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, scoreInPratise.Id);
			sql.ExecuteSql(SqlDeleteScoreInPratise);
		}

		public IList<Core.Business.ScoreInPratise> GetAllScoreInPratise()
		{
			IList<Core.Business.ScoreInPratise> scoreInPratiselist = new List<Core.Business.ScoreInPratise>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllScoreInPratise);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.ScoreInPratise scoreInPratise = new Core.Business.ScoreInPratise();

					if (!reader.IsDBNull(0)) scoreInPratise.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) scoreInPratise.StudentCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) scoreInPratise.YearNum = reader.GetString(2);
					if (!reader.IsDBNull(3)) scoreInPratise.Term = reader.GetInt32(3);
					if (!reader.IsDBNull(4)) scoreInPratise.ScoreNum = reader.GetDecimal(4);
					if (!reader.IsDBNull(5)) scoreInPratise.ScoreCate = reader.GetString(5);

					scoreInPratise.MarkOld();
					scoreInPratiselist.Add(scoreInPratise);
				}
				reader.Close();
			}
			return scoreInPratiselist;
		}
		#endregion
	}
}
